package MODULES.MOD_XML.CONTROLLERS;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import MODULES.MOD_XML.COMMONS.Controls;
import MODULES.MOD_XML.MODELS.Student;
import MODULES.MOD_XML.MODELS.XMLModels;

public class XMLControllers {
    private final XMLModels m_objModels;

    public XMLControllers() {
        m_objModels = new XMLModels();
    }

    public List<Student> ExecOfReadXML(List<Object> lisField, String elementNode, String path) {
        List<Student> lisStudent = null;
        try {
            lisStudent = new ArrayList<Student>();
            List<List<HashMap<String, String>>> lisResult = m_objModels.ReadElement(lisField, elementNode, path);

            // Duyệt danh sách list HashMap để bindding thông tin cho List
            // Student
            HashMap<String, String> objHash = null;
            Student objStudent = null;
            for (int i = 0; i < lisResult.size(); i++)
                for (int j = 0; j < lisResult.get(i).size(); j++) {
                    objHash = lisResult.get(i).get(j);
                    objStudent = new Student();
                    objStudent.setId(Integer.parseInt("" + objHash.get("Id")));
                    objStudent.setRollNo(objHash.get("RollNo"));
                    objStudent.setFullName(objHash.get("FullName"));
                    objStudent.setAge(Integer.parseInt("" + objHash.get("Age")));
                    objStudent.setMark(Double.parseDouble("" + objHash.get("Mark")));
                    lisStudent.add(objStudent);
                }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return lisStudent;
    }

    public boolean ExecOfWriteXML(List<Object> lisField, List<List<Object>> lisValues, String rootNode,
            String elementNode, String path, boolean writeType) {
        try {
            return m_objModels.WriteElement(lisField, lisValues, rootNode, elementNode, path, writeType);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }

    public boolean ExecOfUpdateXML(List<Object> lisField, List<Object> lisValues, String path, String attributeKey,
            String elementNode) {
        try {
            return m_objModels.UpdateElement(lisField, lisValues, path, attributeKey, elementNode);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }

    public List<List<Object>> ExecOfGetListObject(List<Student> lisStudent) {
        List<List<Object>> lisValue = new ArrayList<List<Object>>();
        List<Object> lisO = null;
        for (Student objS : lisStudent) {
            lisO = new ArrayList<Object>();
            lisO.add(objS.getId());
            lisO.add(objS.getRollNo());
            lisO.add(objS.getFullName());
            lisO.add(objS.getAge());
            lisO.add(objS.getMark());
            lisValue.add(lisO);
        }
        return lisValue;
    }

    public DefaultTableModel ConvertListIntoTable(String path) throws Exception {
        // 1. List Field
        List<Object> lisField = new ArrayList<Object>();
        lisField.add("Id");
        lisField.add("RollNo");
        lisField.add("FullName");
        lisField.add("Age");
        lisField.add("Mark");

        // 2. List Student
        List<Student> lisStudent = ExecOfReadXML(lisField, "Student", path);

        // 3. List Rows
        List<Vector<String>> lisRow = new ArrayList<Vector<String>>();
        Vector<String> row = null;
        for (int i = 0; i < lisStudent.size(); i++) {
            row = new Vector<String>();
            row.addElement("" + lisStudent.get(i).getId());
            row.addElement("" + lisStudent.get(i).getRollNo());
            row.addElement("" + lisStudent.get(i).getFullName());
            row.addElement("" + lisStudent.get(i).getAge());
            row.addElement("" + lisStudent.get(i).getMark());
            lisRow.add(row);
        }
        return new Controls().getModels(lisField, lisRow);
    }
}
